﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MobileBSC_SQLite
{
    public static class DbTool<TResult> where TResult : class
    {
        public static TResult ReadSingle(string selectStatement, List<SQLiteParameter> parameters, Func<SQLiteDataReader, TResult> resultCreator)
        {
            TResult result = null;

            using (var conn = new SQLiteConnection(Properties.Settings.Default.MobileBSCConnection))
            {
                conn.Open();
                using (var cmd = new SQLiteCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.CommandText = selectStatement;
                    if (parameters != null)
                        cmd.Parameters.AddRange(parameters.ToArray());

                    using (var rd = cmd.ExecuteReader())
                    {
                        if (rd.HasRows)
                        {
                            rd.Read();
                            result = resultCreator(rd);
                        }
                    }
                }
            }

            return result;
        }
        public static List<TResult> ReadMultiple(string selectStatement, List<SQLiteParameter> parameters, Func<SQLiteDataReader, TResult> resultCreator)
        {
            List<TResult> result = new List<TResult>();

            using (var conn = new SQLiteConnection(Properties.Settings.Default.MobileBSCConnection))
            {
                conn.Open();
                using (var cmd = new SQLiteCommand())
                {
                    cmd.Connection = conn;
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.CommandText = selectStatement;
                    if (parameters != null)
                        cmd.Parameters.AddRange(parameters.ToArray());

                    using (var rd = cmd.ExecuteReader())
                    {
                        if (rd.HasRows)
                        {
                            while (rd.Read())
                            {
                                TResult item = resultCreator(rd);
                                result.Add(item);
                            }
                        }
                    }
                }
            }

            return result;
        }
        public static bool ExecuteCommand(SQLiteCommand command)
        {
            bool done = false;
            using (var conn = new SQLiteConnection(Properties.Settings.Default.MobileBSCConnection))
            {
                conn.Open();
                using (var cmd = command)
                {
                    cmd.Connection = conn;
                    cmd.ExecuteNonQuery();
                    done = true;
                }
            }
            return done;
        }
    }
}
